<?php
/*
 * Copyright Domenic Helfenstein
 */
function getFileExtension($file) {
   $temp = explode(".", $file["name"]);
   return end($temp);
}

function validateFile($file, $maxSize) {
   $allowedExts = array("gif", "jpeg", "jpg", "png");
   $allowedMimes = array("image/gif", "image/jpeg", "image/jpg", "image/pjpeg", "image/png");
   $extension = getFileExtension($file);
   $mime = $file["type"];
   return (in_array($mime, $allowedMimes) && ($file["size"] < $maxSize) && in_array($extension, $allowedExts));
}

function uploadImage($file, $maxSize) {
   $extension = getFileExtension($file);
   if (validateFile($file, $maxSize) && !$file["error"]) {
      $fileName = uniqid().'.'.$extension;
      $destination = ROOT_DIR.DS.'uploaded'.DS.$fileName;
      if(move_uploaded_file($file["tmp_name"], $destination)) {
         return $fileName;
      } else {
         echo '<p class="bg-danger">Unbekannter Fehler</p>';
      }
   } else {
      echo '<p class="bg-danger">Es wurde versucht eine Datei, die kein Bild ist, heraufzuladen!</p>';
   }
   return false;
}
?>
